//
//  JBWEmoticonKeyboardView.swift
//  新浪微博
//
//  Created by 季伯文 on 2017/7/10.
//  Copyright © 2017年 bowen. All rights reserved.
//

import UIKit

class JBWEmoticonKeyboardView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //刷新最近表情页
    func reloadRecentEmoticons(){
        let indexPath = IndexPath(item: 0, section: 0)
        pageView.reloadItems(at: [indexPath])
        
    }

    //设置视图
    private func setupUI(){
        //设置大小
        self.frame.size = CGSize(width: JBWSCREENW, height: 216)
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        
        //添加控件
        addSubview(bottomView)
        addSubview(pageView)
        addSubview(pageControl)
        bottomView.snp.makeConstraints { (make) in
            make.height.equalTo(35)
            make.left.bottom.right.equalTo(self)
        }
        
        pageView.snp.makeConstraints { (make) in
            make.left.top.right.equalTo(self)
            make.bottom.equalTo(bottomView.snp.top)
        }
        
        pageControl.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.bottom.equalTo(pageView).offset(5)
        }
        //监听底部按钮点击
        bottomView.closure = {[weak self](type) in
            switch type {
            case .recent:
                print("最近")
            case .normal:
                print("默认")
            case .emoji:
                print("emoji")
            case .lxh:
                print("浪小花")
            
            }
            
            //滚动到指定组
            let indexPath = IndexPath(item: 0, section: type.rawValue)
            self?.pageView.scrollToItem(at: indexPath, at: UICollectionViewScrollPosition.left, animated: false)
            //设置分页指示器切换
            self?.setupPageControl(indexPath: indexPath)
        }
    }
    
    //懒加载控件
    fileprivate lazy var bottomView: JBWEmoticonBottomView = JBWEmoticonBottomView()
    
    //自定义表情键盘配图
    fileprivate lazy var pageView: JBWEmoticonPageView = {
        let view = JBWEmoticonPageView()
        view.backgroundColor = self.backgroundColor
        view.delegate = self
        //分页
        view.isPagingEnabled = true
        //弹簧效果
        view.bounces = true
        //隐藏滚动条
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        return view
        
    }()
    
    //分页指示器
    fileprivate lazy var pageControl: UIPageControl = {
        let view = UIPageControl()
        //设置总页数
        view.numberOfPages = JBWEmoticonTools.share.allEmoticons[1].count
        //当前页数
        view.currentPage = 0
        //选中时颜色
//        view.currentPageIndicatorTintColor = 
        view.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "currentPageImage")
        view.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "pageImage")
        //取消用户相互影响
        view.isUserInteractionEnabled = false
        //当numberOfPages== 1 时默认隐藏
        view.hidesForSinglePage = true
        return view
    }()
}

//代理
extension JBWEmoticonKeyboardView: UICollectionViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        //获取中心x点
        let centerx = JBWSCREENW * 0.5 + scrollView.contentOffset.x
        //判断哪个cell显示的多
        let visibCells = pageView.visibleCells.sorted { (cell, cell2) -> Bool in
            return cell.frame.origin.x < cell2.frame.origin.x
        }
        if visibCells.count == 2 {
            //分别获取第一个cell和最后已给cell
            let firstcell = visibCells.first!
            let lastcell = visibCells.last!
            let indexPath: IndexPath
            
            if firstcell.frame.contains(CGPoint(x: centerx, y: 0)) {
//                print("第一个cell显示的多")
                indexPath = pageView.indexPath(for: firstcell)!
                } else {
//                print("第二个cell显示的多")
                indexPath = pageView.indexPath(for: lastcell)!
            }
            
            //得到最终的indexPath
            bottomView.setupSelectButton(tag: indexPath.section)
            setupPageControl(indexPath: indexPath)
        }
    }
    
    //设置pageControl的公共方法
    fileprivate func setupPageControl(indexPath: IndexPath){
        pageControl.numberOfPages = JBWEmoticonTools.share.allEmoticons[indexPath.section].count
        pageControl.currentPage = indexPath.item
    }
}
